package com.hancher.gamelife2.menstrual;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;

import com.hancher.common.androidutil.DensityUtil;
import com.hancher.gamelife2.R;
import com.hancher.gamelife2.databinding.DialogEditMenstrualBinding;
import com.lxj.xpopup.core.CenterPopupView;
import com.noober.background.drawable.DrawableCreator;

/**
 * 描述 : 记录开始结束信息 <br/>
 * 时间 : 2024/4/29 18:05 <br/>
 * 作者 : Hancher ytu_shaoweijie@163.com
 */
public class EditMenstrualDialog extends CenterPopupView {


    private DialogEditMenstrualBinding binding;

    interface OnSelectFaceListener {
        void onSelect(MenstrualPO uploadFinishVO);
    }

    private MenstrualPO menstrualPO;
    private Long recordTime;

    /**
     * 状态:-1未记录；0开始；1结束
     */
    private int status = -1;
    private OnSelectFaceListener listener;

    public EditMenstrualDialog(@NonNull Context context, MenstrualPO menstrualPO, Long recordTime, OnSelectFaceListener listener) {
        super(context);
        this.menstrualPO = menstrualPO;
        this.recordTime = recordTime;
        this.listener = listener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_edit_menstrual;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        binding = DialogEditMenstrualBinding.bind(getPopupImplView());

        if (menstrualPO != null) {
            binding.etName.setText(menstrualPO.getName());
            binding.etRemark.setText(menstrualPO.getDescription());
            status = menstrualPO.getStatus();
        }

        binding.btnSubmitMenstrual.setOnClickListener(v -> {
            MenstrualPO data2;
            data2 = menstrualPO == null ? new MenstrualPO() : menstrualPO;
            data2.setCreateTime(System.currentTimeMillis());
            data2.setUpdateTime(System.currentTimeMillis());
            data2.setName(binding.etName.getText().toString());
            data2.setDescription(binding.etRemark.getText().toString());
            data2.setStatus(status);
            data2.setRecordTime(recordTime);
            listener.onSelect(data2);
            dismiss();
        });

        changeRecordState(status);
        // -1未开始； 0开始； 1结束；2进行中
        binding.tvStart.setOnClickListener(v -> {
            changeRecordState(status == 0 ? -1 : 0);
        });
        binding.tvEnd.setOnClickListener(v -> {
            changeRecordState(status == 1 ? -1 : 1);
        });
    }

    private void changeRecordState(int state) {
        status = state;
        //设置button圆角背景
        Drawable drawableUnselect = new DrawableCreator.Builder()
                .setCornersRadius(DensityUtil.dp2px(getContext(), 150))
                .setSolidColor(Color.WHITE)
                .setStrokeColor(Color.parseColor("#4169e1"))
                .setStrokeWidth(DensityUtil.dp2px(getContext(), 1))
                .build();
        Drawable drawableSelect = new DrawableCreator.Builder()
                .setCornersRadius(DensityUtil.dp2px(getContext(), 150))
                .setSolidColor(Color.parseColor("#4169e1"))
                .setStrokeColor(Color.parseColor("#4169e1"))
                .setStrokeWidth(DensityUtil.dp2px(getContext(), 1))
                .build();
        binding.tvStart.setBackground(state == 0 ? drawableSelect : drawableUnselect);
        binding.tvEnd.setBackground(state == 1 ? drawableSelect : drawableUnselect);
        binding.tvStart.setTextColor(state == 0 ? Color.WHITE : Color.parseColor("#4169e1"));
        binding.tvEnd.setTextColor(state == 1 ? Color.WHITE : Color.parseColor("#4169e1"));
    }


}
